Calculate current age from the Date of Birth

For an approximate age in years, the below simple calculation can be also used

For a Windows (XrServer) Deployment:

@Assign Age_Years = int((Date - Date_of_Birth)/ 365.25)

For a JavaScript Engine Deployment:

@Assign Age_Years = int((Date - GetDays(Date_of_Birth)) / 365.25)

The following example shows how to calculate the exact age of the person (in whole years and days, taking leap years into account) relative to the current system date, as well as calculating the date of the person's next birthday.

For a Windows (XrSever) Deployments:

@Do Date_of_Birth
@Date System_Date
@DecodeDate Date_of_Birth,DOB_Day,DOB_Month,DOB_Year
@DecodeDate System_Date, System_Day, System_Month, System_Year
@Assign Age_Years = System_Year - DOB_Year
@If EncodeDate(DOB_Day, DOB_Month,1900) > EncodeDate(System_Day, System_Month,1900)
  @Assign Age_Years = Age_Years + 1
  @Assign Next_Birthday = EncodeDate(DOB_Day, DOB_Month, System_Year + 1)
@Else
  @Assign Next_Birthday = EncodeDate(DOB_Day, DOB_Month, System_Year)
@EndIf
@Assign Age_Days = System_Date - Next_Birthday

For a JavaScript Engine Deployments:

@Do Date_of_Birth
@Date System_Date
@DecodeDate Date_of_Birth,DOB_Day,DOB_Month,DOB_Year
@DecodeDate System_Date, System_Day, System_Month, System_Year
@Assign Age_Years = GetDays(System_Year) - GetDays(DOB_Year)
@If EncodeDate(DOB_Day, DOB_Month,1970) > EncodeDate(System_Day, System_Month,1970)
  @Assign Age_Years = Age_Years + 1
  @Assign Next_Birthday = EncodeDate(DOB_Day, DOB_Month, System_Year + 1)
@Else
  @Assign Next_Birthday = EncodeDate(DOB_Day, DOB_Month, System_Year)
@EndIf
@Assign Age_Days = GetDays(System_Date) - GetDays(Next_Birthday)

Three Date Attributes are used in the above example: System_Date, Date_of_Birth and Next_Birthday. The date of birthday is captured in Date_of_Birth. The calculated age result is then stored in Age_Years and Age_Days, with the next birthday stored in Next_Birthday.

Also for JavaScript Deployment, the following JavaScript code can be used to define a JavaScript Function (inside a Knowledge Builder Procedure) to calculate the age in years:

function (birthday:n) : n
var today = new Date();
var age = today.getFullYear() - birthday.getFullYear();
if (birthday.getMonth() > today.getMonth)
  age--;
else if ((birthday.getMonth() == today.getMonth()) && (birthday.getDate() > today.getDate()))
  age--;
return age; 

Predefined functions